/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include    "rtos-wsn.h"
#include	"wsn-taskid.h"
#include 	"wsn-demosrc-bind.h"

CONTROL_OUT_TOGGLE(void, on, uint16);
CONTROL_OUT_TOGGLE(void, off, uint16);

uint8 input_ch;
uint8 demo_run_flag = 0;
static uint16 led_num = 0;

static struct wsn_timer wsn_demo_timer;

void wsn_demo_timer_func(struct wsn_timer *timer)
{
	input_ch++;
	WSN_NewTask(DEMO_TASKID, 0, 0);	/* new demo task */
}



int Wsn_DemoTask(int event)
{

	WSN_DebugOut("Wsn_DemoTask Sched\n\r");

	OUTPUT_ADDATA(input_ch);//INPUT_DEMODST_SPID() 	/* data output port */

	led_num = ~led_num;
	if(led_num == 0)
		CONTROL_TOGGLE(off, 0x02);	/* control output port */
	else
		CONTROL_TOGGLE(on, 0x02);
	
	if ( demo_run_flag == 1 ){
		WSN_StartTimer(&wsn_demo_timer, CONSTAINT_MINI_TO_TICKS(500));
	}
  return 1;
}
/* Demo Init */
//CONTROL_DEMOSRC_STDinit
void CONTROL_IN_STD(init)(void)
{
	WSN_InitTimer(&wsn_demo_timer, wsn_demo_timer_func);
}

/* Demo Start */
//CONTROL_DEMOSRC_STDstart
void CONTROL_IN_STD(start)(void)
{
	if (demo_run_flag == 1)
		return;
	WSN_DebugOut("Demo Start ... \n\r");	
	demo_run_flag = 1;
	WSN_StartTimer(&wsn_demo_timer, CONSTAINT_MINI_TO_TICKS(1000));
}

///* Demo Stop */
////CONTROL_DEMOSRC_STDstop
void CONTROL_IN_STD(stop)(void)
{
	if (demo_run_flag == 0)
		return;
	
	demo_run_flag = 0;			/* stop flag when in task */
	WSN_StopTimer(&wsn_demo_timer);	/* stop when out of task and timer started already */

	WSN_DebugOut("Demo Stop ... ...\n\r");

}

